package com.cac.demo.controller.standardSalesReturn;

import com.cac.demo.dao.standardSalesReturn.StandardSalesReturnDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("StandardSalesReturnRest")
public class StandardSalesReturnRest {
    @Autowired
    private StandardSalesReturnDao standardSalesReturnDao;

    @PostMapping("standardReturnQuery")
    public HashMap standardReturnQuery(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        HashMap pageParameter = (HashMap) map.get("pageParameter");
        HashMap searchParams = (HashMap) map.get("searchParams");
        Integer pageNum = (Integer) pageParameter.get("page");
        Integer pageSize = (Integer) pageParameter.get("rows");
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap> standardReturnQuery = standardSalesReturnDao.standardReturnQuery(searchParams);
        PageInfo pageInfo = new PageInfo(standardReturnQuery);
        hashMap.put("retCode","200");
        hashMap.put("retData",standardReturnQuery);
        hashMap.put("totalCount",pageInfo.getTotal());
        hashMap.put("totalPage",pageInfo.getPages());
        return hashMap;
    }
    //退库查询交接单数据
    @PostMapping("selectDeliverPlanStandard")
    public HashMap selectDeliverPlanStandard(@RequestBody HashMap map) {
        HashMap hashMap = new HashMap();
        HashMap pageParameter = (HashMap) map.get("pageParameter");
        HashMap searchParams = (HashMap) map.get("searchParams");
        Integer pageNum = (Integer) pageParameter.get("page");
        Integer pageSize = (Integer) pageParameter.get("rows");
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap> selectDeliverPlanStandard = standardSalesReturnDao.selectDeliverPlanStandard(searchParams);
        PageInfo pageInfo = new PageInfo(selectDeliverPlanStandard);
        hashMap.put("retCode","200");
        hashMap.put("retData",selectDeliverPlanStandard);
        hashMap.put("totalCount",pageInfo.getTotal());
        hashMap.put("totalPage",pageInfo.getPages());
        return hashMap;
    }

    //标准件退库保存
    @PostMapping("standardReturnSave")
    @Transactional
    public HashMap standardReturnSave(@RequestBody HashMap map){
        HashMap hashMap = new HashMap();
        List<HashMap> hashMaps = (List<HashMap>) map.get("prOrderMidTable");
        for(HashMap hashmap: hashMaps){
            hashmap.put("ynWorkLineFlag",map.get("ynWorkLineFlag"));
            hashmap.put("appearanceStatus",map.get("appearanceStatus"));
            hashmap.put("returnType",map.get("returnType"));
            hashmap.put("returnQty",map.get("returnQty"));
            hashmap.put("account",hashmap.get("receiveEmpCode"));
            hashmap.put("empinfoid",hashmap.get("receiveEmpCode"));
            hashmap.put("text",map.get("text"));
            hashmap.put("departid",map.get("departid"));
            hashmap.put("accordingNo",map.get("accordingNo"));
            hashmap.put("equipStatus",map.get("equipStatus"));
            hashmap.put("maxStorageDate",map.get("maxStorageDate"));
            hashmap.put("sealDate",map.get("sealDate"));
            hashmap.put("remark",map.get("remark"));
            standardSalesReturnDao.standardReturnSave(hashmap);
        }
        hashMap.put("retCode","200");
        hashMap.put("retMsg","创建退库单成功！");
        return hashMap;
    }
}
